<?php
/**
 * Created by PhpStorm.
 * User: JackMao  codermao@outlook.com
 * Date: 2019/5/4
 * Time: 4:08 PM
 */
include 'phpqrcode.php';
if (!$_POST) {
    exit("请求数据不能为空");
}
$link = $_POST['qrtext'];
$invite_code = $_POST['invite_code'];

$link = urldecode($link);
if (!$link) {
    exit('link 不能为空');
}
$fileBase = md5($link);
$filename = $fileBase . ".png";
if (file_exists($filename)) {
    echo "/gen_qrcode/" . $filename;
    exit;
}

QRcode::png($link, $filename, 'L', 8, 0);

/* 其实就是把上面生成的图片再进一步调整大小 这里建议第一步生成的图片要稍微大一些 否则放大会模糊 */
/* 设置宽高 */
$width = 100;
$height = 100;
/* 获取新尺寸 */
list($width_orig, $height_orig) = getimagesize($filename);
/* 调整比例 */
$ratio_orig = $width_orig / $height_orig;
if ($width / $height > $ratio_orig) {
    $width = $height * $ratio_orig;
} else {
    $height = $width / $ratio_orig;
}
/* 重新取样 */
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
/* 输出 */
imagepng($image_p, $filename, 0);


/* 图片一  */
$path_1 = 'sidchain-release.png';
/* 创建图片对象 */
$image_1 = imagecreatefrompng($path_1);
$image_2 = imagecreatefrompng($filename);
/* 合成图片 这里要调整第三个参数和第四个参数 根据你的背景留空位置来写即可 */
imagecopymerge($image_1, $image_2, 600, 1500, 0, 0, imagesx($image_2), imagesy($image_2), 100);
/* 输出合成图片  */
$color = imagecolorallocatealpha($image_1, 0, 0, 0, 0);
imagettftext($image_1, 30, 0, 300, 470, $color, "Arial.ttf", $invite_code);

imagepng($image_1, $filename, 5);
imagedestroy($image_1);
echo "/gen_qrcode/" . $filename;
